let activeEffect;
class Dep {
  constructor(value) {
    this.subscribers = new Set();
    this._value = value;
  }

  get value() {
    this.depend();
    return this._value;
  }

  set value(newVal) {
    this._value = newVal;
    this.notify();
  }

  depend() {
    if (activeEffect) {
      this.subscribers.add(activeEffect);
    }
  }
  notify() {
    this.subscribers.forEach((effect) => {
      effect();
    });
  }
}

function watchEffect(effect) {
  activeEffect = effect;
  effect();
  activeEffect = null; // 保证了activeEffect只被收集一次
}

function ref(value) {
  return new Dep(value);
}

const count = ref(123);
// 第一次watch时，内层函数也会执行一遍
watchEffect(() => {
  console.log("count: ", count.value);
});

count.value = 100; // 被监听函数会再次执行
